package com.fpost.test;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import com.fpost.dao.impl.ArticleDAO;
import com.fpost.dao.impl.DictionaryDAO;
import com.fpost.domain.Article;
import com.fpost.manager.ArticleManager;

public class DictionaryUtil {

	public static void main(String[] args) {
		
//		List<String> listOfWords = getDictionary();
//		for (String string : listOfWords) {
//			System.out.println(string);
//		}
//		System.out.println(listOfWords.size());
		

//		List<Long> ids = new ArticleDAO().getCurrentArticleIds();
//		for (Long id : ids) {
//			System.out.println(id);
//		}		
		
		//List<Long> articleIds = new ArticleDAO().getCurrentArticleIds();
		List<Long> articleIds = new ArticleDAO().getCurrentArticleIdsByUsername("User1");
		fillDictionaryTable(articleIds);
	}
	
	public static List<String> getDictionary() {

		String sourceFile = "C:/Archive/FPOST_BACKUP/corncob_lowercase.txt";
		List<String> listOfWords = new ArrayList<String>();
		try {
			FileInputStream fis = new FileInputStream(sourceFile);
			DataInputStream dis = new DataInputStream(fis);
			BufferedReader br = new BufferedReader(new InputStreamReader(dis));			
						
			String strLine;	
			while ((strLine = br.readLine()) != null) {				
				listOfWords.add(strLine.trim());						
			}
			dis.close();
		} catch (IOException e) {
			System.err.println("Error: " + e.getMessage());
		}
		return listOfWords;
	}
	
	public static void fillDictionaryTable(List<Long> articleIds) {		
		
		ArticleManager articleManager = new ArticleManager();
		DictionaryDAO dictionaryDAO = new DictionaryDAO();		
		
		System.out.println("found " + articleIds.size() + " articles");
		int count = 0;
		long startTime = System.currentTimeMillis();
		for (Long articleId : articleIds) {
			Article article = articleManager.getArticleById(articleId.longValue());
			dictionaryDAO.addNewWords(articleId);
			count++;
			//if (count%100 == 0) {
			if(true) {
				System.out.println(count  + "  " + articleId + "  " + (System.currentTimeMillis() - startTime)/60000 + " min");
			}
		}
	}

}
